package middleware

import (
	"errors"
	"fmt"
	"ordersystem/config"
	"ordersystem/util"

	"github.com/gofiber/fiber/v2"
)

func UserAuthorizationCheckMiddleware(ctx *fiber.Ctx) error {

	resourceData := ctx.Cookies("id", "") + ctx.Cookies("identify", "") + ctx.Cookies("telphone", "") + ctx.Cookies("creator", "") + ctx.Cookies("expirestime", "")
	destinationData := util.DoubleMd5Salt(resourceData)

	if destinationData != ctx.Cookies("token") {
		fmt.Println(ctx.Cookies("token"))
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}
	return ctx.Next()
}

func UserAdminIdentifyCheckMiddleware(ctx *fiber.Ctx) error {
	if ctx.Cookies("identify") == "2" {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}
	return ctx.Next()
}

func UserSuperAdminIdentifyCheckMiddleware(ctx *fiber.Ctx) error {
	if ctx.Cookies("identify") != "0" {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}
	return ctx.Next()
}
